package com.wawayaya.parentteacher.ui;


import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;



import com.wawayaya.api.ApiResponseData;
import com.wawayaya.api.interf.ApiResponseJsonListener;
import com.wawayaya.api.remote.ApiConstans;
import com.wawayaya.api.remote.ApiParentTeacher;
import com.wawayaya.base.BaseActivity;
import com.wawayaya.base.BaseFragment;
import com.wawayaya.parentteacher.Constants;
import com.wawayaya.parentteacher.TeacherParentEvent;
import com.wawayaya.parentteacher.adapter.SelectBookFragmentAdapter;
import com.wawayaya.parentteacher.adapter.TpBookCoverAdapter;
import com.wawayaya.parentteacher.bean.Books;
import com.wawayaya.util.TLog;
import com.wawayaya.util.event.ShelfEvent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import wawayaya.kids_iread.R;

/**
 * Created by wang_qian on 2015/12/7.
 */
public class SelectBookActivity extends BaseActivity implements ApiResponseJsonListener
{
    private ViewPager Pager;
    private PagerTabStrip tabStrip;
    private Button    mBtnAddToBasket;
    private Button mBtnPush;
    private RecyclerView mRcMore;
    private TpBookCoverAdapter mMoreBookCoverAdapter;
    private TextView mtvTitle;


    private ArrayList<BaseFragment> fragmentList;
    private SelectBookFragmentAdapter pageAdatper;

    private ArrayList<String>     mTitleList;

    private List<Books>      mCurLibBookList;

    private boolean         mIsZh;

    @Override
    protected void init(Bundle savedInstanceState) {
        setContentView(R.layout.tp_selectbook);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {
        Pager = (ViewPager)findViewById(R.id.pager);
        tabStrip = (PagerTabStrip)findViewById(R.id.pagertab);
        mBtnAddToBasket = (Button)findViewById(R.id.btn_add_to_basket);
        mBtnPush = (Button)findViewById(R.id.btn_recoommend);
        mtvTitle = (TextView)findViewById(R.id.tv_title);
        mtvTitle.setText(getResources().getString(R.string.book_lib));

        mRcMore = (RecyclerView)findViewById(R.id.rv_morebooks);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,4);
        mRcMore.setLayoutManager(gridLayoutManager);
        mRcMore.setHasFixedSize(true);




    }



    @Override
    public void initData() {
        //获取选书页数据
        ApiParentTeacher.getBookListToChoose(context, this);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onBackPressed() {
        if (mRcMore.getVisibility() == View.VISIBLE) {
            mRcMore.setAdapter(null);
            if (mCurLibBookList != null) mCurLibBookList.clear();
            mtvTitle.setText(getResources().getString(R.string.book_lib));
            mRcMore.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }


    @Override
    public void success1(ApiResponseData result) {
        switch(result.getRquestType()) {
            case ApiConstans.TP_LIB:
                parseLib(result.getRetinfo());
                if (fragmentList != null && fragmentList.size() > 0) {
                    pageAdatper = new SelectBookFragmentAdapter(getSupportFragmentManager(), fragmentList, mTitleList);
                    Pager.setAdapter(pageAdatper);
                }
                break;
            case ApiConstans.TP_AGE_LIB_BOOKS:
            case ApiConstans.TP_ENG_LIB_BOOKS:
            case ApiConstans.TP_SERIES_LIB_BOOKS:
            case ApiConstans.TP_GRADE_LIB_BOOKS:
            case ApiConstans.TP_RECOMMEND_LIB_BOOKS:
            case ApiConstans.TP_THEME_LIB_BOOKS:
                parseBooks(result.getRetinfo());
                if (mCurLibBookList != null && mCurLibBookList.size() > 0) {
                    mMoreBookCoverAdapter = new TpBookCoverAdapter(this, mIsZh, mCurLibBookList);
                    mRcMore.setAdapter(mMoreBookCoverAdapter);
                }
                break;
        }
    }

    @Override
    public void success0(ApiResponseData result) {
        switch(result.getRquestType()) {
            case ApiConstans.TP_LIB:


                break;
        }

    }

    @Override
    public void error(ApiResponseData result) {
        switch(result.getRquestType()) {
            case ApiConstans.TP_LIB:


                break;
        }
    }

    private void parseBooks(JSONObject retinfo) {
        if (mCurLibBookList != null)  mCurLibBookList.clear();
        if (retinfo == null) return;
        if (retinfo.has("books")) {
            JSONArray bookarray = retinfo.optJSONArray("books");
            if (bookarray != null && bookarray.length() > 0) {

                int book_num =  bookarray.length();
                for (int j = 0; j < book_num; j++) {
                    JSONObject bookjo = bookarray.optJSONObject(j);
                    if (bookjo != null) {
                        if (mCurLibBookList == null) mCurLibBookList = new ArrayList<Books>();
                        Books book = new Books();
                        book.setBookId(bookjo.optLong("book_id"));
                        book.setNameEn(bookjo.optString("book_name"));
                        book.setNameZh(bookjo.optString("book_name"));
                        book.setThumb(bookjo.optString("book_cover_uri"));
                        mCurLibBookList.add(book);
                    }
                }
            }

        }


    }

    private void parseLib(JSONObject   retinfo) {
        if (mTitleList != null)  mTitleList.clear();
        if (fragmentList != null) fragmentList.clear();
        if (retinfo == null) return;
        try {
            if (retinfo.has("lib")) {

                JSONObject jolib = retinfo.optJSONObject("lib");
                if (jolib != null) {
                    if (mTitleList == null) mTitleList = new ArrayList<String>();
                    if (fragmentList == null) fragmentList = new ArrayList<BaseFragment>();

                    String jsonstr = "";
                    if (jolib.has("Recommend")) {
                        mTitleList.add(getResources().getString(R.string.recomend));
                        jsonstr = jolib.optString("Recommend");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr, Constants.BOOK_LIB_CLASS_RECOMMEND);
                        fragmentList.add(newfragment);
                    }
                    if (jolib.has("Series")) {
                        mTitleList.add(getResources().getString(R.string.series));
                        jsonstr = jolib.optString("Series");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr, Constants.BOOK_LIB_CLASS_SERIES);
                        fragmentList.add(newfragment);
                    }
                    if (jolib.has("Theme")) {
                        mTitleList.add(getResources().getString(R.string.theme));
                        jsonstr = jolib.optString("Theme");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr,Constants.BOOK_LIB_CLASS_THEME);
                        fragmentList.add(newfragment);
                    }
                    if (jolib.has("Age")) {
                        mTitleList.add(getResources().getString(R.string.ages));
                        jsonstr = jolib.optString("Age");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr, Constants.BOOK_LIB_CLASS_AGE);
                        fragmentList.add(newfragment);
                    }
                    if (jolib.has("Grade")) {
                        mTitleList.add(getResources().getString(R.string.grads));
                        jsonstr = jolib.optString("Grade");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr, Constants.BOOK_LIB_CLASS_GRADE);
                        fragmentList.add(newfragment);
                    }
                    if (jolib.has("English books")) {
                        mTitleList.add(getResources().getString(R.string.english_book));
                        jsonstr = jolib.optString("English books");
                        SelectBookFragment newfragment = SelectBookFragment.newInstance(jsonstr, Constants.BOOK_LIB_CLASS_ENGLISH);
                        fragmentList.add(newfragment);

                    }


                }

            }
        } catch (Exception e) {

        }

    }

    /**
     * event回调
     *
     * @param event
     */
    public void onEvent(TeacherParentEvent event) {
        switch (event.eventType) {
            case TeacherParentEvent.TYPE_SHOW_RECOMMEND_BOOKS:
            case TeacherParentEvent.TYPE_SHOW_SERIES_BOOKS:
            case TeacherParentEvent.TYPE_SHOW_THEME_BOOKS:
            case TeacherParentEvent.TYPE_SHOW_AGE_BOOKS:
            case TeacherParentEvent.TYPE_SHOW_GRADE_BOOKS:
            case TeacherParentEvent.TYPE_SHOW_ENGLISHBOOK_BOOKS:
                showBooks(event);
                break;
        }
    }



    private void showBooks(TeacherParentEvent event) {
        mRcMore.setVisibility(View.VISIBLE);
        mtvTitle.setText(event.strInfo);
        switch (event.eventType) {
            case TeacherParentEvent.TYPE_SHOW_RECOMMEND_BOOKS:
                ApiParentTeacher.getBookListFromRecommendLib(context, this, event.Id);
                break;
            case TeacherParentEvent.TYPE_SHOW_SERIES_BOOKS:
                ApiParentTeacher.getBookListFromSeriesLib(context, this, event.Id);
                break;
            case TeacherParentEvent.TYPE_SHOW_THEME_BOOKS:
                ApiParentTeacher.getBookListFromThemeLib(context, this, event.Id);
                break;

            case TeacherParentEvent.TYPE_SHOW_AGE_BOOKS:
                ApiParentTeacher.getBookListFromAgeLib(context, this, event.Id);
            case TeacherParentEvent.TYPE_SHOW_GRADE_BOOKS:
                ApiParentTeacher.getBookListFromGradeLib(context, this, event.Id);
                break;
            case TeacherParentEvent.TYPE_SHOW_ENGLISHBOOK_BOOKS:
                ApiParentTeacher.getBookListFromEnglishLib(context, this, event.Id);
                break;
        }



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mCurLibBookList != null) mCurLibBookList.clear();

    }

}
